package com.levi.listener;

import com.levi.cache.UserCache;
import com.levi.domain.User;
import com.levi.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @author shentong
 * @since 2022/3/8 9:34 PM 监听器，在项目启动的时候执行一些初始化操作
 */
@Slf4j
@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	private UserService userService;

	@Autowired
	private RBloomFilter bloomFilter;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		try {
			SseEmitter sseEmitter = new SseEmitter(0L);
			log.info("项目启动了！！！！");
			log.info("get ip =>>>" + InetAddress.getLocalHost().getHostAddress());
			List<User> userList = userService.selectList(null);
			userList.forEach(UserCache::insertUsers);
			log.info("用户初始化完成！！！" + userList);
			bloomFilter.add("9");
			bloomFilter.add("6");
			bloomFilter.add("8");
		}
		catch (UnknownHostException e) {
			log.error(e.toString());
		}
	}

}
